package com.jfinal.weixin.model;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.msg.in.event.EventInMsg;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import com.jfinal.weixin.sdk.utils.StringUtil;


/**
 * Created by chengzhichao on 16/5/4.
 */
public class User extends Model<User> {
    public static final User dao = new User();

    /**
     * 根据openid 获取用户实体
     *
     * @param openid 微信识别openid
     * @return 符合条件的用户实体
     */
    public User getUserByOpenid(String openid) {
        return super.findFirst("select * from user where openid=?", new Object[]{openid});
    }

    /**
     * 根据openid查询该用户存在的记录数
     *
     * @param openid
     * @return 符合条件的用户实体
     */
    public int isJoined(String openid) {
        return super.find("select id from user where openid=?", new Object[]{openid}).size();
    }

    /**
     * 根据id查询该用户存在的记录数
     *
     * @param id
     * @return 符合条件的用户实体
     */
    public int isJoined(int id) {
        return super.find("select id from user where id=?", new Object[]{id}).size();
    }

    /**
     * 查看一共有多少个人关注了本公众号.
     *
     * @return 符合条件的用户实体
     */
    public int getcount() {
        return super.find("select id from user where subscribe=1").size();
    }


    public User setUserByApiResult(ApiResult apiResult) {
        User user = new User();
        user.set("openid", apiResult.getStr("openid"));
        user.set("nickname", apiResult.getStr("nickname"));
        user.set("sex", apiResult.getInt("sex"));
        user.set("city", apiResult.getStr("city"));
        user.set("headimgurl", apiResult.getStr("headimgurl"));
        user.set("subscribe", apiResult.getInt("subscribe"));
        return user;
    }

    public User updateUserByApiResult(ApiResult apiResult,String openid){
        User user = getUserByOpenid(openid);
        user.set("nickname", apiResult.getStr("nickname"));
        user.set("sex", apiResult.getInt("sex"));
        user.set("city", apiResult.getStr("city"));
        user.set("headimgurl", apiResult.getStr("headimgurl"));
        return user;
    }

    /**
     * 获取当天
     * @return
     */
    public int gettodayadd(){
        int size = super.find("SELECT id FROM `user` where concat(CURDATE(),' 00:00:00') < time and concat(CURDATE(),' 23:59:59') > time and subscribe=1").size();
        return size;
    }

    public int gettodayremove(){
        int size = super.find("SELECT id FROM `user` where concat(CURDATE(),' 00:00:00') < time and concat(CURDATE(),' 23:59:59') > time and subscribe=0").size();
        return size;
    }

    /**
     * 修改订阅状态
     * @param openid
     * @return 0 从取消订阅到订阅状态  1从订阅状态取消
     */
    public int updateDingyue(String openid){
       User user =  getUserByOpenid(openid);
        if(user.getInt("dingyue")==0) {
            user.set("dingyue", 1).update();
        }else{
            user.set("dingyue", 0).update();
        }
        return user.getInt("dingyue");
    }


    public OutTextMsg setSubscribeMsgByOpenidAndEvent(String openid, EventInMsg eventInMsg) {
        OutTextMsg outMsg = new OutTextMsg(eventInMsg);
        if (User.dao.isJoined(openid) > 0) {
            User user = User.dao.getUserByOpenid(openid).set("subscribe", 1);
            user.update();
            int id = user.getInt("id");
            outMsg.setContent("我2,肥来料,稀客稀客,再莫走.第" + id + "位关注我们公众号的老朋友.敢不敢跟我说话,敢不敢陪我聊聊天?" + StringUtil.BKMTITLE);
        } else {
            ApiResult apiResult = UserApi.getUserInfo(eventInMsg.getFromUserName());
            User user = User.dao.setUserByApiResult(apiResult);
            user.save();
            int id = user.getInt("id");
            outMsg.setContent("握草,这么有眼光.您是第" + id + "位关注我们公众号的红安人.敢不敢跟我说话,敢不敢陪我聊聊天?" + StringUtil.BKMTITLE);
        }
        return (outMsg);
    }

    public boolean issend(String openid){
        int a =  super.find("select id from user where openid=? and issend=? and concat(CURDATE(),' 00:00:00') < sendtime and concat(CURDATE(),' 23:59:59') > sendtime", new Object[]{openid,1}).size();
        return a>0;
    }

}
